﻿<UserControl 
    x:Class="myManga_App.Views.ReaderView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:vm="clr-namespace:myManga_App.ViewModels"
    xmlns:Button="clr-namespace:myManga_App.Resources.Theme.Buttons"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:mangaObjects="clr-namespace:myMangaSiteExtension.Objects;assembly=myMangaSiteExtension"
    xmlns:Converters="clr-namespace:myManga_App.Converters"
    xmlns:CoreControls="clr-namespace:System.Windows.Controls;assembly=Core"
    xmlns:Controls="clr-namespace:myManga_App.Controls"
    x:Name="userControl"
    mc:Ignorable="d" 
    d:DesignHeight="400"
    d:DesignWidth="600"
    Background="{DynamicResource window-background}" 
    Foreground="{DynamicResource window-foreground}"
    FocusManager.FocusedElement="{Binding ElementName=userControl}"
    Controls:InputBindingsBehavior.HasInputBindingPrecedence="True">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/myManga;component/Resources/Localization/Dictionary_en-US.xaml"/>
                <ResourceDictionary Source="/myManga;component/Themes/CoreResourceDictionary.xaml"/>
                <ResourceDictionary Source="/myManga;component/Themes/Button/MetroButton.xaml"/>

                <ResourceDictionary Source="/myManga;component/Themes/ListBox/ListBoxes.xaml"/>
                <ResourceDictionary Source="/myManga;component/Themes/ListBox/ListBoxItems.xaml"/>
                <ResourceDictionary Source="/myManga;component/Themes/ScrollBar/ScrollBar.xaml"/>
                <ResourceDictionary Source="/myManga;component/Themes/Slider/Slider.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <Converters:LoadImageFromChapterArchive x:Key="LoadImageFromChapterArchive"/>
            <Converters:BooleanInverter x:Key="BooleanInverter"/>
        </ResourceDictionary>
    </UserControl.Resources>
    <UserControl.InputBindings>
        <KeyBinding Command="{Binding PrevPageCommand}" Key="Left"/>
        <KeyBinding Command="{Binding PrevPageCommand}" Key="BrowserForward"/>

        <KeyBinding Command="{Binding NextPageCommand}" Key="Right"/>
        <KeyBinding Command="{Binding NextPageCommand}" Key="BrowserBack"/>

        <KeyBinding Command="{Binding ReloadPageImageCommand}" Key="F5"/>

        <KeyBinding Command="{Binding ResetPageZoomCommand}" Key="D0" Modifiers="Ctrl"/>
        <KeyBinding Command="{Binding ResetPageZoomCommand}" Key="NumPad0" Modifiers="Ctrl"/>
        <KeyBinding Command="{Binding DecreasePageZoomCommand}" Key="OemMinus" Modifiers="Ctrl"/>
        <KeyBinding Command="{Binding DecreasePageZoomCommand}" Key="Subtract" Modifiers="Ctrl"/>
        <KeyBinding Command="{Binding IncreasePageZoomCommand}" Key="OemPlus" Modifiers="Ctrl"/>
        <KeyBinding Command="{Binding IncreasePageZoomCommand}" Key="Add" Modifiers="Ctrl"/>
    </UserControl.InputBindings>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition MinHeight="24" Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="48"/>
            <ColumnDefinition/>
            <ColumnDefinition Width="48"/>
        </Grid.ColumnDefinitions>
        <Button:MetroToggleButton 
            x:Name="PageListDropdown"
            Grid.ColumnSpan="3"
            IsHitTestVisible="{Binding IsOpen, Converter={StaticResource BooleanInverter}, ElementName=PageListPopup, Mode=OneWay}">
            <Button:MetroToggleButton.Content>
                <StackPanel Orientation="Horizontal">
                    <TextBlock 
                        Text="{Binding MangaObject.Name, StringFormat={}{0}\ :, FallbackValue=Un-Named\ Manga\ :}" 
                        FontWeight="Bold"/>
                    <TextBlock Text=" " Visibility="{Binding ChapterObject.Name, TargetNullValue=Collapsed}" />
                    <TextBlock 
                        x:Name="ChapterObjectName"
                        Text="{Binding ChapterObject.Name, StringFormat={}{0}}" 
                        Visibility="{Binding ChapterObject.Name, TargetNullValue=Collapsed}" 
                        TextDecorations="Underline"/>
                    <TextBlock Text=" "/>
                    <TextBlock 
                        Text="{Binding ChapterObject.Volume, StringFormat={}Vol.{0}}" 
                        Visibility="{Binding Text, TargetNullValue=Collapsed, RelativeSource={RelativeSource Self}}"/>
                    <TextBlock Text=" "/>
                    <TextBlock>
                        <TextBlock.Text>
                            <MultiBinding StringFormat="Ch.{0}.{1}">
                                <Binding Path="ChapterObject.Chapter" FallbackValue="0"/>
                                <Binding Path="ChapterObject.SubChapter" FallbackValue="0"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                    <TextBlock Text=" "/>
                    <TextBlock 
                        Text="{Binding SelectedPageObject.PageNumber, FallbackValue={}Page: 0, StringFormat={}Page: {0}}"/>
                    <TextBlock Text=" of "/>
                    <TextBlock 
                        Text="{Binding ChapterObject.Pages.Count, FallbackValue={}0, StringFormat={}{0}}"/>
                </StackPanel>
            </Button:MetroToggleButton.Content>
        </Button:MetroToggleButton>
        <Popup
            x:Name="PageListPopup"
            AllowsTransparency="True"
            Width="{Binding ActualWidth, ElementName=PageListDropdown, Mode=OneWay}"
			PlacementTarget="{Binding ElementName=PageListDropdown}"
            PlacementRectangle="{Binding ElementName=PageListDropdown}"
			IsOpen="{Binding IsChecked, ElementName=PageListDropdown}"
            PopupAnimation="Fade"
            Grid.ColumnSpan="3" 
            StaysOpen="False">
            <StackPanel
                Orientation="Vertical"
                Background="{DynamicResource window-background}">
                <ListBox
                    x:Name="PageList"
                    ItemsSource="{Binding ChapterObject.Pages}"
                    SelectedItem="{Binding SelectedPageObject}" 
                    IsSynchronizedWithCurrentItem="True" 
                    ItemContainerStyle="{DynamicResource ReaderView_PageListBoxItem}" 
                    ScrollViewer.VerticalScrollBarVisibility="Disabled"
                    Background="{DynamicResource window-background}"
                    SourceUpdated="ImageContent_SourceUpdated"
                    SelectionChanged="PageList_SelectionChanged">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel IsItemsHost="True" Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>
                <Separator/>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock 
                        Text="{Binding Value, ElementName=PageZoomSlider, StringFormat=Zoom\ Amount:\ {0:0.0}x}"
                        FontSize="14"
                        VerticalAlignment="Center"
                        Margin="0,0,3,0"/>
                    <Slider
                        x:Name="PageZoomSlider"
                        Grid.Column="1"
                        Orientation="Horizontal"
                        Minimum="0.5"
                        Maximum="2"
                        Value="{Binding Path=PageZoom, Mode=TwoWay, FallbackValue=1}" 
                        TickPlacement="BottomRight"
                        TickFrequency="0.1"
                        LargeChange="0.5" 
                        SmallChange="0.1" 
                        IsMoveToPointEnabled="True" 
                        IsSnapToTickEnabled="True"/>
                    <Button:MetroButton
                        Grid.Column="2"
                        Content="Reset"
                        FontSize="14"
                        Padding="5,1"
                        Command="{Binding ResetPageZoomCommand}"/>
                </Grid>
            </StackPanel>
        </Popup>
        <Button:MetroButton
            Grid.Column="0"
            Grid.Row="1"
            VerticalContentAlignment="Center"
            HorizontalContentAlignment="Center" 
            FontFamily="{DynamicResource OpenIconic}"
            Content="{DynamicResource oi-arrow-thick-left}"
            Command="{Binding PrevPageCommand}"
            FontSize="24"/>
        <ScrollViewer
            x:Name="ImageContentScrollViewer"
            Grid.Column="1"
            Grid.Row="1" 
            PanningMode="Both" 
            IsManipulationEnabled="True" 
            CanContentScroll="True" 
            HorizontalScrollBarVisibility="Auto" 
            VerticalScrollBarVisibility="Auto"
            VerticalContentAlignment="Center"
            HorizontalContentAlignment="Center">
            <ScrollViewer.ContextMenu>
                <ContextMenu>
                    <MenuItem Command="{Binding ReloadPageImageCommand}" Header="(F5) Reload Page Image">
                        <MenuItem.Icon>
                            <TextBlock
                                Text="{DynamicResource oi-cloud-download}" 
                                FontFamily="{DynamicResource OpenIconic}" 
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <Separator/>
                    <MenuItem Command="{Binding IncreasePageZoomCommand}" Header="(Ctrl+=) Zoom In">
                        <MenuItem.Icon>
                            <TextBlock
                                Text="{DynamicResource oi-plus}" 
                                FontFamily="{DynamicResource OpenIconic}" 
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Command="{Binding DecreasePageZoomCommand}" Header="(Ctrl+-) Zoom Out">
                        <MenuItem.Icon>
                            <TextBlock
                                Text="{DynamicResource oi-minus}" 
                                FontFamily="{DynamicResource OpenIconic}" 
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Command="{Binding ResetPageZoomCommand}" Header="(Ctrl+0) Reset Zoom">
                        <MenuItem.Icon>
                            <TextBlock
                                Text="{DynamicResource oi-reload}" 
                                FontFamily="{DynamicResource OpenIconic}" 
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center"/>
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </ScrollViewer.ContextMenu>
            <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5">
                <Image
                    x:Name="ImageContent"
                    Stretch="UniformToFill" 
                    IsHitTestVisible="False"
                    Binding.SourceUpdated="ImageContent_SourceUpdated"
                    Binding.TargetUpdated="ImageContent_SourceUpdated" 
                    Height="{Binding Source.PixelHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}"
                    Width="{Binding Source.PixelWidth, Mode=OneWay, RelativeSource={RelativeSource Self}}"
                    Margin="3">
                    <Image.Source>
                        <MultiBinding 
                        Converter="{StaticResource LoadImageFromChapterArchive}"
                        NotifyOnSourceUpdated="True"
                        NotifyOnTargetUpdated="True">
                            <Binding Path="MangaObject"/>
                            <Binding Path="ChapterObject"/>
                            <Binding Path="SelectedPageObject"/>
                        </MultiBinding>
                    </Image.Source>
                    <Image.LayoutTransform>
                        <ScaleTransform 
                        ScaleX="{Binding Path=PageZoom, Mode=OneWay, FallbackValue=1}" 
                        ScaleY="{Binding Path=PageZoom, Mode=OneWay, FallbackValue=1}"/>
                    </Image.LayoutTransform>
                </Image>
                <Border
                    BorderBrush="{DynamicResource window-background}"
                    BorderThickness="5">
                    <Border.Effect>
                        <BlurEffect Radius="3"/>
                    </Border.Effect>
                </Border>
            </Grid>
        </ScrollViewer>
        <Button:MetroButton
            Grid.Column="2"
            Grid.Row="1"
            VerticalContentAlignment="Center"
            HorizontalContentAlignment="Center"
            FontFamily="{DynamicResource OpenIconic}"
            Content="{DynamicResource oi-arrow-thick-right}"
            Command="{Binding NextPageCommand}"
            FontSize="24"/>
    </Grid>
</UserControl>
